<?php
header( "Content-Type:text/html; charset=utf-8" );
require_once("config.inc.php");
require_once("weibo.class.php");
require_once("db.class.php");
ignore_user_abort();
set_time_limit(0);
$wb = new weibo(APP_KEY, SECRET_KEY);
$wb->setUser( USER_NAME , USER_PASS );
$db = new db($site_Db);
//取出未处理的请求
$requestList = $db->fetchrows("SELECT * FROM request WHERE deal=0");
if(!empty($requestList)) {
	foreach( $requestList as $request )
	{
		$id = $request['id'];
		$tid = $request['tid'];
		$uid = $request['uid'];
		$uname = $request['uname'];
		$type = $request['type'];
		$data = $request['data'];

		//echo dirname(__FILE__).'/'.$type.'.class.php';
		
		if(file_exists(dirname(__FILE__).'/'.$type.'.class.php')) {
			require_once ($type.'.class.php');
			$handle = new $type(&$wb,&$db,$data,$option_form[$type]);
			if($handle->run()) {
				$comment = $handle->get_Response_data();
			} else {
				$comment = "用户" . $uname . "，您的请求未被成功执行，存在以下错误：" . $handle->get_Error_msg()."。" . $handle->get_Request_form();
			}
		} else {
			$comment = "用户" . $uname . "，您的请求未被成功执行，存在以下错误：不存在的请求类型！";
		}
		
		//echo $comment;
		//print_r($handle->get_Response_day());
		//print_r($handle->get_Response_month());

		if(!empty($comment)) {
			if($db->query("INSERT INTO comment VALUES(null, '{$tid}', '{$comment}', ".time().", 0)"))
				echo "Add a comment.\n";
		}

		if($db->query("UPDATE request SET deal=1 WHERE id={$id}"))
			echo "Handle a request.\n";
	}
	
	//暂停3秒后执行
	sleep(3);
	@exec('/usr/local/php5/bin/php -f /usr/local/www/weibo/shijian/handle_comment.php');
} else {
	echo "No request queue.\n";
}

$db->close();

?>